Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO

Module mod_Common
    'Public MyServerCn As OleDbConnection
    Public MyLocalStore As String = "StockTaking"
    Public MyServerTVNWCn As SqlConnection
    Public MyServerCn As SqlConnection
    Public MyLocalCn As SqlConnection
    Public MyDBCn As OleDbConnection
    Public MyDBIEOCn As OleDbConnection
    ' Public MyLocalConnection As New LocalConnection
    Public MyDBConnection As New DBConnection
    Public MyDBIEOConnection As New DBIEOConnection
    Public MyServerConnection As New ServerConnection
    Public MySVRTVNWConnection As New SVRConnectionTVNW
    Public MyMenuFlag As Integer = 0

    Public MyStatusString As String = ""
    Public MyProcessString As String = ""
    Public MyPath As String = Application.StartupPath & "\"
    Public MyOnlineFlg As Boolean = False
    Public StopProgram As Boolean = False

    Public MyStep As Integer = 0
    Public MySubStep As Integer = 0
    Public MyShelfList As DataTable
    Public MyEmployeeList As DataTable
    Public ReadingString As String = ""
    Public MyShelf As String = ""
    Public MyIDPart As String = ""
    Public MyPart As String = ""
    Public MyOprCode As String = ""
    Public MyOprName As String = ""
    Public MyLOCode As String = ""
    Public MyLOName As String = ""
    Public MyCounter As Integer = 0
    Public CompleteShelfFlg As Boolean = False
    Public MySplitArray() As String
    Public MyReadString As String

    Public MyCustInf As String = ""
    Public MyPartName As String = ""
    'Public MyStartID As String = ""
    'Public MyEndID As String = ""
    Public MyQty As String = ""
    Public MyBQty As Integer = 0
    Public MySQty As Integer = 0
    Public MyExtraQty As String = ""
    Public MyWOQty As Integer = 0
    Public MyWO As String = ""
    Public MySubWO As String = ""
    Public MyCode As String = ""
    Public MyInputTime As Date
    Public MyRoHSMarking As String = ""
    Public FormatBarcodeLabel As String = ""
    Public MyCustPartName As String = ""
    Public SubWOFlg As Boolean = False
    Public MyCDItem As String = ""
    Public MyTVNPartCode As String = ""
    Public MyTVNPartCodeFlg As Boolean = False
    Public MyCartonSize As String = ""
    Public MyAddTVNPartName As String = "NONE"
    Public MyPN As String = ""
    Public MyRPID As String = ""
    Public MyRPFlg As String = ""
    Public DeliveryTable As DataTable
    Public InventoryTable As DataTable
    Public SHTPartName As String = ""
    Public SHTWO As String = ""
    Public SHTQTY As String = ""
    Public SHTDQTY As String = ""
    Public SHTWHWIP As String = ""
    Public MyShipDate As Date
    Public SHTComment As String = ""
    Public SHTPATime As String = ""
    Public SHTID As String = ""

    Public MyPartLOC As String = ""
    Public ds2Obj As CRDataSet2 = New CRDataSet2
    Public dsObj As CRDataSet = New CRDataSet
    Public dsRMObj As CRDataSet = New CRDataSet
    Public dsSubObj As CRDataSet = New CRDataSet
    Public dsR As CRDataSet = New CRDataSet
    Public dsW As CRDataSet = New CRDataSet
    Public dsS As CRDataSet = New CRDataSet
    Public dsHS As CRDataSet = New CRDataSet
    Public RePO As CRDataSet2 = New CRDataSet2
    Public MyBMoWO As Boolean = False
    Public MyDisplayFunctionFlg As String = ""
    Public StockNo As String = "" ' "WH_01"
    Public PartTolerance As Double = 0.02
    Public CurrentUserName As String = ""
    Public CurrentPassword As String = ""
    Public CurrentCode As String = ""
    Public CurrentProcess As String = ""
    Public MyNeedQty As Integer = 0
    Public MyPrintQty As Integer = 0
    Public MySShelf As String = ""
    Public MyRemainQty As Integer = 0
    Public MyPrintIDB As String = ""
    Public PIDRecord As Integer = 0
    Public PIDRecord1 As Integer = 0
    Public DeliveryFlg As Integer = 0
    Public MyACode As String = ""
    Public MyDOpCode As String = ""
    Public MyGetPartNote As String = ""
    Public MyWHRec As String = ""
    Public MyWHDel As String = ""
    Public CurrentACode As String = ""
    Public StringAID As String = ""
    Public JumpIDFlg As String = 0
    Public DN As String = ""
    Public FuncMS As String = 0
    Public ClickLOC As String = ""
    Public LogfileAdd As String = ""
    Public IEOAdd As String = ""
    Public PrinterID As String = ""
    Public PrinterA4 As String = ""
    Public LTPartdes As String = ""
    Public LTflg As Boolean = False
    Public f As Integer = 0
    Declare Function WriteProfileString Lib "kernel32" _
     Alias "WriteProfileStringA" _
     (ByVal lpszSection As String, _
     ByVal lpszKeyName As String, _
     ByVal lpszString As String) As Long
    Public Const HWND_BROADCAST = &HFFFF
    Public Const WM_WININICHANGE = &H1A
    Declare Function SendMessage Lib "user32" _
        Alias "SendMessageA" _
        (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lparam As String) As Long
    Public RMDP As Boolean = False
    Public RMDPFlg As Boolean = False
    Public ACheckResistance As String = ""
    Public QW As Integer = 0
    Public WHInv As Integer = 0
    Public WHInvFlg As Boolean = False
    Public InputMaterialFlg As Integer = 0
    Public GARM As String = ""


    Public Sub SetDefaultPrinter(ByVal printerName As String, ByVal DriverName As String, ByVal PrinterPort As String)
        Dim DeviceLine As String
        Dim r As Long
        ' Dim l As Long
        DeviceLine = printerName & "," & DriverName & "," & PrinterPort
        ' Store the new printer information in the [WINDOWS] section of
        ' the WIN.INI file for the DEVICE= item
        r = WriteProfileString("windows", "Device", DeviceLine)
        ' Cause all applications to reload the INI file:
        ' l = SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, "windows")
    End Sub



    Public Sub ErrRecording(ByVal MyProcessName As String, ByVal MyErrSource As String, ByVal MyErrString As String)
        Dim MyRecordFS As FileStream = Nothing
        Dim MyTempFile As String = ""
        Dim MySW As StreamWriter = Nothing
        Dim MySystemTime As String
        MySystemTime = Format(Now(), "yyMMddHHmmss")
        MyTempFile = LogfileAdd & "\LFBom.txt"
        Try
            MyRecordFS = New FileStream(MyTempFile, FileMode.OpenOrCreate, fileAccess.Write)
            Dim MyWriteString As String
            MySW = New StreamWriter(MyRecordFS)
            MySW.BaseStream.Seek(0, SeekOrigin.End)
            MyWriteString = StockNo & "| " & Now() & "|" & CurrentUserName & Space(2) & MyProcessName & Space(2) & MyErrSource & Space(2) & MyErrString
            MySW.WriteLine(MyWriteString)
            MySW.Flush()
        Catch
        Finally
            Try
                If Not MySW Is Nothing Then MySW.Close()
                If Not MyRecordFS Is Nothing Then MyRecordFS.Close()
            Catch
            End Try
        End Try
    End Sub

    Public Function CheckOnline() As Boolean
        Dim MyResult As Boolean = False
        Try
            If MyServerCn.State = ConnectionState.Closed Then
                MyServerCn.Open()
                MyResult = True
            End If
        Catch ex As Exception
        End Try
        Return MyResult
    End Function
    Public Function Shift3DateTimeChange(ByVal MyDateTime As DateTime) As DateTime
        Dim MyDateTimeStr As String = MyDateTime.ToString("dd-MMM-yyyy HH:mm:ss tt")
        Dim MyTimeStr As String = Mid(MyDateTimeStr, 13)
        Dim MyHour As Integer = DatePart(DateInterval.Hour, MyDateTime)
        Dim MyDate As Date = DateValue(Mid(MyDateTimeStr, 1, 11))
        If MyHour >= 0 AndAlso MyHour < 6 Then
            Dim MyNewDateTimeStr As String = DateAdd(DateInterval.Day, -1, MyDate).ToString("dd-MMM-yyyy") & " " & MyTimeStr
            Return DateTime.ParseExact(MyNewDateTimeStr, "dd-MMM-yyyy HH:mm:ss tt", Application.CurrentCulture)
        Else
            Return MyDateTime
        End If
    End Function

    Public Function CalShiftTime(ByVal MyCurrentTime As DateTime) As Integer
        Dim MyCalTime As Integer = DatePart(DateInterval.Hour, MyCurrentTime)
        If MyCalTime >= 6 AndAlso MyCalTime < 14 Then 'Shift 1
            Return 1
        ElseIf MyCalTime >= 14 AndAlso MyCalTime < 22 Then 'Shift 2
            Return 2
        ElseIf MyCalTime >= 22 Then  'shift 3
            Return 3
        ElseIf MyCalTime >= 0 AndAlso MyCalTime < 6 Then 'shift 3
            Return 3
        End If
    End Function
    Public Function ConvertServerDateTime(ByVal MyTime As Date) As String
        Dim MyStringTime As String = CType(MyTime, DateTime).ToString("dd-MMM-yyyy hh:mm:ss tt")
        Return MyStringTime
    End Function

    Public Function ConvertServerDate(ByVal MyTime As Date) As String
        Dim MyStringTime As String = CType(MyTime, DateTime).ToString("dd MMM yyyy")
        Return MyStringTime
    End Function
End Module

